//
//  PKPlayInfoViewController.m
//  PianKe
//
//  Created by ybq on 15/12/25.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKPlayInfoViewController.h"
@interface PKPlayInfoViewController ()

@property (nonatomic, strong) UIImageView *mainImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UILabel *likeLabel;
@property (nonatomic, strong) UIButton *commentButton;
@property (nonatomic, strong) UILabel *commentLabel;
@property (nonatomic, strong) UIButton *downLoadButton;
@property (nonatomic, strong) UISlider *timeSlider;
@property (nonatomic, strong) UILabel *surplusTimeLabel;

@end

@implementation PKPlayInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(45, 40, kWidth - 90, kWidth - 90)];
    [self.view addSubview:_mainImageView];
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, _mainImageView.frame.origin.y + _mainImageView.frame.size.height + 50, kWidth - 100, 30)];
    _titleLabel.font = [UIFont boldSystemFontOfSize:20];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_titleLabel];
    _likeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _likeButton.frame = CGRectMake(105, _titleLabel.frame.origin.y + 55, 15, 15);
    [_likeButton setImage:[UIImage imageNamed:@"like"] forState:(UIControlStateNormal)];
    [self.view addSubview:_likeButton];
    _likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_likeButton.frame.origin.x + 23, _likeButton.frame.origin.y + 3, 30, 10)];
    _likeLabel.font = [UIFont systemFontOfSize:10];
    _likeLabel.textColor = [UIColor lightGrayColor];
    _likeLabel.text = @"111";
    [self.view addSubview:_likeLabel];
    _commentButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _commentButton.frame = CGRectMake(kWidth - 115, _likeButton.frame.origin.y, 15, 15);
    [_commentButton setImage:[UIImage imageNamed:@"comment"] forState:(UIControlStateNormal)];
    [self.view addSubview:_commentButton];
    _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_commentButton.frame.origin.x + 23, _likeLabel.frame.origin.y, 30, 10)];
    _commentLabel.font = [UIFont systemFontOfSize:10];
    _commentLabel.textColor = [UIColor lightGrayColor];
    _commentLabel.text = @"222";
    [self.view addSubview:_commentLabel];
    _downLoadButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _downLoadButton.frame = CGRectMake(50, _likeLabel.frame.origin.y + 35, 20, 20);
    [_downLoadButton setImage:[UIImage imageNamed:@"download"] forState:(UIControlStateNormal)];
    [self.view addSubview:_downLoadButton];
    _timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(92, _downLoadButton.frame.origin.y + 10, kWidth - 185, 5)];
    _timeSlider.minimumTrackTintColor = [UIColor grayColor];
    [_timeSlider setThumbImage:[UIImage imageNamed:@"shu"] forState:(UIControlStateNormal)];
    [_timeSlider addTarget:self action:@selector(timeChange:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_timeSlider];
    _surplusTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 72, _downLoadButton.frame.origin.y + 7, 40, 10)];
    _surplusTimeLabel.font = [UIFont systemFontOfSize:10];
    _surplusTimeLabel.textColor = [UIColor lightGrayColor];
    [self.view addSubview:_surplusTimeLabel];
    
    
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(playerPlaying) userInfo:nil repeats:YES];
}

- (void)timeChange:(UISlider *)sender {
    PKMusicPlayer *player = [PKMusicPlayer sharePlayer];
    [player seekToTime:sender.value];
    [player play];
}

- (void)playerPlaying {
    PKMusicPlayer *player = [PKMusicPlayer sharePlayer];
    NSInteger currentTime = player.currentTime;
    NSInteger finishTime = player.finishTime;
    NSInteger surplusTime = finishTime - currentTime;
    _surplusTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld",surplusTime / 60, surplusTime % 60];
    _timeSlider.maximumValue = finishTime;
    _timeSlider.value = currentTime;
    if (surplusTime == 0 && finishTime != 0) {
        [player finishPlay];
    }
}

- (void)setViewWithListModel:(PKRadioDetailModel *)model {
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:[UIImage imageNamed:@"guangpan"]];
    _titleLabel.text = model.title;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
